package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.MoneyDetail;
import org.springblade.modules.app.service.IMoneyDetailService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 账户明细 控制器
 *
 * @author BladeX
 * @since 2020-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/moneydetail")
@Api(value = "账户明细", tags = "账户明细接口")
public class MoneyDetailController extends BladeController {

    private final IMoneyDetailService moneyDetailService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入moneyDetail")
    public R<MoneyDetail> detail(MoneyDetail moneyDetail) {
        MoneyDetail detail = moneyDetailService.getOne(Condition.getQueryWrapper(moneyDetail));
        return R.data(detail);
    }

    /**
     * 分页 账户明细
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入moneyDetail")
    public R<IPage<MoneyDetail>> list(MoneyDetail moneyDetail, Query query) {
        IPage<MoneyDetail> pages = moneyDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(moneyDetail));
        return R.data(pages);
    }

    /**
     * 自定义分页 账户明细
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入moneyDetail")
    public R<IPage<MoneyDetail>> page(Page page, MoneyDetail moneyDetail) {
        BladeUser bladeUser = AuthUtil.getUser();
        LambdaQueryWrapper<MoneyDetail> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(wrapper -> wrapper.eq(MoneyDetail::getUserid, bladeUser.getUserId()));
        queryWrapper.orderByDesc(MoneyDetail::getCtime);
        IPage<MoneyDetail> pages = moneyDetailService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 新增 账户明细
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入moneyDetail")
    public R save(@Valid @RequestBody MoneyDetail moneyDetail) {
        return R.status(moneyDetailService.save(moneyDetail));
    }

    /**
     * 修改 账户明细
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入moneyDetail")
    public R update(@Valid @RequestBody MoneyDetail moneyDetail) {
        return R.status(moneyDetailService.updateById(moneyDetail));
    }

    /**
     * 新增或修改 账户明细
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入moneyDetail")
    public R submit(@Valid @RequestBody MoneyDetail moneyDetail) {
        return R.status(moneyDetailService.saveOrUpdate(moneyDetail));
    }


    /**
     * 删除 账户明细
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(moneyDetailService.removeByIds(Func.toLongList(ids)));
    }


}
